<template>
  <PageWrapper
    title="任务提交"
    contentBackground
    content=" 表单页用于向用户收集提交任务内容。"
    contentClass="p-4"
  >
    <BasicForm @register="register" />
  </PageWrapper>
</template>
<script lang="ts">
  import { BasicForm, useForm } from '/@/components/Form';
  import { defineComponent, unref, onMounted,ref ,computed} from 'vue';
  import { submit } from './data';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { PageWrapper } from '/@/components/Page';
  import { useRouter } from 'vue-router';
  import { GetContApi,TaskSubApi } from '/@/api/learningcorner/task';
  import { TaskContResultModel } from '/@/api/learningcorner/model/taskModel'
  import { useUserStore } from '/@/store/modules/user';
  import { AffectedRowsResultModel } from '/@/api/learningcorner/model/userModel';

  export default defineComponent({
    components: { BasicForm, PageWrapper },
    setup() {

      const { currentRoute } = useRouter();
      const { createMessage } = useMessage();

      const params = unref(currentRoute).query;
      console.log(params);

      const userStore = useUserStore();
      const realName = computed(() => {
        const { realName } = userStore.getUserInfo || {};
        return realName;
      });                                                       // 获取用户名

      console.log(realName.value)
      console.log(new Date().getTime())



    onMounted(()=>{
        customSubmitFunc1()
    })


      const [register, { validate, setProps,setFieldsValue,getFieldsValue }] = useForm({

        labelCol: {
          span: 7,
        },
        wrapperCol: {
          span: 10,
        },
        schemas: submit,
        actionColOptions: {
          offset: 8,
        },
        submitButtonOptions: {
          text: '提交',
        },
        showResetButton:false,
        submitFunc: customSubmitFunc,
      });

      async function customSubmitFunc1(){
          try{
            const params = unref(currentRoute).query;
            let result: TaskContResultModel = await getCont(params.id);
            setFieldsValue(result);
          }catch(error){}
      };





      async function customSubmitFunc() {

        await validate()


        const value = getFieldsValue();
        const params = unref(currentRoute).query;
        const values = {tid:params.id,jrid:realName.value,content:value.submitcontent,sub_time:new Date().setFullYear(new Date().getFullYear() - 20 )};
        try {
          let result: AffectedRowsResultModel = await TaskSubApi(values);
          setProps({
            submitButtonOptions: {
              loading: true,
            },
          });
          setTimeout(() => {
            setProps({
              submitButtonOptions: {
                loading: false,
              },
            });
            if(result.affectedRows==1){
              createMessage.success('提交成功！');
            }
            else{
              createMessage.error('提交失败');
            }

          }, 2000);
        } catch (error) {}
      }

      return {
        register,
        realName,
      };

    },
  });
  function getCont(id:string){
    return GetContApi({ id })
  }
</script>
<style lang="less" scoped>
  .form-wrap {
    padding: 24px;
    background-color: @component-background;
  }
</style>
